package edu.seu.mart.dao.impl;

import edu.seu.mart.dao.BaseDao;
import edu.seu.mart.dao.IHistoryDao;
import edu.seu.mart.entity.SystemRequest;
import edu.seu.mart.entity.SystemRequestHolder;
import edu.seu.mart.model.History;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by samsung on 2015/4/1.
 */
@Repository
public class HistoryDao extends BaseDao<History> implements IHistoryDao {

    @Override
    public List<History> findHistoryByUserId(int userId) {
        SystemRequest systemRequest = new SystemRequest();
        systemRequest.setPageOffset(0);
        systemRequest.setPageSize(10);
        systemRequest.setOrder("desc");
        systemRequest.setSort("browseTime");
        SystemRequestHolder.initRequestHolder(systemRequest);
        return this.find("from History where userId = '" + userId + "'").getDatas();
    }

    @Override
    public int isBrowsed(int userId, int goodsId) {
        if (this.list("from History where userId = '" + userId + "' and goodsId = '" + goodsId + "'").isEmpty()) {
            return 0;
        } else {
            return this.list("from History where userId = '" + userId + "' and goodsId = '" + goodsId + "'").get(0).getId();
        }
    }

    @Override
    public int getUserCount(int userId) {
        return (int) this.find("from History where userId = '" + userId + "'").getTotal();
    }
}
